<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      const canvas = document.createElement("canvas");
      canvas.width = 800;
      canvas.height = 800;
      document.body.append(canvas);

      const ctx = canvas.getContext("2d");
      const img = new Image();
      img.crossOrigin = "anonymous";
      img.src =
        "https://img.chuapp.com//wp-content/Picture/2017-03-08/58bfb93d25c08.jpg?imageView2/5/w/587/h/330";
      img.onload = function () {
        ctx.drawImage(img, 0, 0);

        const imageData = ctx.getImageData(0, 0, 600, 400);
        console.log("imageData:", imageData.data.length);
        for (let i = 0; i < imageData.data.length; i = i + 4) {
          const r = imageData.data[i];
          const g = imageData.data[i + 1];
          const b = imageData.data[i + 2];
          const a = imageData.data[i + 3];

          const avg = (r + g + b) / 3;
          console.log("avg:", avg);

          imageData.data[i] = avg;
          imageData.data[i + 1] = avg;
          imageData.data[i + 2] = avg;
        }
        ctx.putImageData(imageData, 0, 0);
      };
    </script>
  </body>
</html>
